﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using linkedin_bot_demo.Models;
using System.Collections.ObjectModel;
using System.Windows.Input;
using linkedin_bot_demo.Common;
using linkedin_bot_demo.Views;
using System.Windows;
using System.Threading;
using linkedin_bot_demo.Service;

namespace linkedin_bot_demo.ViewModels
{
    internal class GroupsViewModel : ViewModel
    {
        public GroupsViewModel()
        {
            PostGroupModel postModel = new PostGroupModel();
        }

        public ObservableCollection<GroupModel> Groups { get { return _groups; } }
        private readonly ObservableCollection<GroupModel> _groups = new ObservableCollection<GroupModel>();

        private DelegateCommand _reloadGroupsCommand;
        public ICommand ReloadGroupsCommand
        {
            get { return _reloadGroupsCommand ?? (_reloadGroupsCommand = new DelegateCommand(ReloadGroups)); }
        }

        private ProgressContextViewModel _loadingGroupsProgressContext;
        public ProgressContextViewModel LoadingGroupsProgressContext
        {
            get { return this._loadingGroupsProgressContext; }
            set
            {
                if (this._loadingGroupsProgressContext != value)
                {
                    this._loadingGroupsProgressContext = value;
                    this.OnPropertyChanged("LoadingGroupsProgressContext");
                }
            }
        }

        private ProgressContextViewModel _sendToGroupProgressContext;
        public ProgressContextViewModel SendToGroupProgressContext
        {
            get { return this._sendToGroupProgressContext; }
            set
            {
                if (this._sendToGroupProgressContext != value)
                {
                    this._sendToGroupProgressContext = value;
                    this.OnPropertyChanged("SendToGroupProgressContext");
                }
            }
        }

        private DelegateCommand _sendToGroupsCommand;
        public ICommand SendToGroupsCommand
        {
            get { return _sendToGroupsCommand ?? (_sendToGroupsCommand = new DelegateCommand(OnSendToGroupsCommandExecute)); }
        }
        private void OnSendToGroupsCommandExecute()
        {
            GroupModel selectedGroup = this.SelectedGroup;
            AccountModel selectedAccount = this.SelectedAccount;

            if (selectedGroup == null)
            {
                MessageBox.Show("Please choose group");

                return;
            }

            if (selectedAccount == null)
            {
                MessageBox.Show("Please choose account");

                return;
            }

            this.SendToGroupProgressContext = new ProgressContextViewModel();

            TaskScheduler uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
            Task.Factory.StartNew(() =>
            {
                using (LinkedinApiClient api = new LinkedinApiClient(selectedAccount.AccessToken))
                {
                    api.CreateGroupPost(selectedGroup.Id, PostTitle, PostText);
                }
            })
            .ContinueWith(t =>
            {
                this.SendToGroupProgressContext = null;
                MessageBox.Show("Post was created");
            }, uiTaskScheduler);
        }

        private AccountModel _selectedAccount;
        public AccountModel SelectedAccount
        {
            get { return this._selectedAccount; }
            set
            {
                if (this._selectedAccount != value)
                {
                    this._selectedAccount = value;
                    OnPropertyChanged("SelectedAccount");

                    ReloadGroups();
                }
            }
        }

        private GroupModel _selectedGroup;
        public GroupModel SelectedGroup
        {
            get { return this._selectedGroup; }
            set
            {
                this._selectedGroup = value;
                OnPropertyChanged("SelectedGroup");
            }
        }

        public string PostTitle { get; set; }
        public string PostText { get; set; }

        private void ReloadGroups()
        {
            var selectedAccount = SelectedAccount;

            this.LoadingGroupsProgressContext = new ProgressContextViewModel();
            this.Groups.Clear();

            if (selectedAccount != null)
            {
                TaskScheduler uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
                Task.Factory.StartNew(() =>
                {
                    using (LinkedinApiClient api = new LinkedinApiClient(SelectedAccount.AccessToken))
                    {
                        return api.GetGroups().ToArray();
                    }
                })
                .ContinueWith(t =>
                {
                    try
                    {
                        foreach (GroupModel group in t.Result)
                        {
                            this.Groups.Add(group);
                        }
                    }
                    finally
                    {
                        this.LoadingGroupsProgressContext = null;
                    }
                }, uiTaskScheduler);
            }
        }
    }
}
